Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1f  Compiler Error  The static main method can not access the non-static method m.  
2b  Prints: 2,3  Primitive type variables are passed to methods by value. In other words, only the value of the variable is passed to the method so the method has no access to the variable itself. If the method modifies a primitive value that has been passed to the method as a parameter, then the modification does not impact the variable outside of the method. Instead, only the value of the local copy is modified. For that reason, the method m does not change the value of the variable y in the main method. However, method m does indeed have direct access to the member variable x and its value is modified by method m and the modification is seen within the main method.  
3a  b  c  d  1  2  3  4  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. The final, synchronized, native, and strictfp modifiers can not appear in the declaration of an abstract method and can not be applied to an abstract method declared in an interface.  
4a  e  abstract  public  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. An abstract method can not also be declared private, static, final, native, strictfp, or synchronized so the same restriction applies to methods declared within an interface.  
5 All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface.  
6a  Prints: float,float  An argument of type long can be widened to a float or a double. Since the float type is more specific than a double the float type is selected before double.  
7d  Runtime error at line 10.  The compiler accepts the explicit cast at line 10. However, Base is not a subclass of Sub; therefore, a runtime exception is thrown.  
8a  Prints: AAA  Class A has only one implementation of method m1. Class B overloads method m1 with a second implementation. Class C overloads method m1 with a third implementation. Even though c1, c2 and c3 are all instances of class C, the reference type is always A so the overload methods declared in the subclasses are not accessible. For that reason, the implementation of method m1 declared in class A is always invoked.  
9f  Runtime error at line 10.  The object referenced by obj is of type "Base[]", and can not be cast to type "Sub[]".  
10b  Prints: P.printS1 Q.printS2  Static method Q.printS1 hides the static method P.printS1 in the super class P. Instance method Q.printS2 overrides the instance method P.printS2. Due the the differences between the hiding of static methods and the overridding of instance methods the invocation of the two methods in P.printS1S2 produces different results. The method invocation expression printS1 results in the invocation of the hidden super class method P.printS1. The method invocation expression printS2 results in the invocation of the overridding sub class method Q.printS2.  
11b  Prints: ABC  One of the overloaded methods is selected based on the type of the argument. The type of the argument is determined by the reference type. The type of c1 is A. The type of c2 is B. The type of c3 is C.  
12b  Prints: BBABBA  The first method call, m(null,null), calls method m with two null literals that have type null. Both null literals are promoted to type B because B is more specific than type A. As a result, m(B x, B y) is invoked. The second method call, m(a=null,b=null), calls m with a first parameter of type A and a second parameter of type B. As a result, m(A x, B y) is invoked because the parameter types are matched exactly. The third method call invokes m(B x, A y) because the parameter types match exactly.  
13f  Compiles and runs without error.  Although line 12 appears to be the source of a compiler error it is not. In line 12, the reference named base actually refers to an object of type Sub. Therefore, the reference to the object may be cast to type Sub.  
14b  Prints: I10.s10,I20.s20,I20  This is a trick question. Class C20 inherits ambiguous declarations of the "name" field. As long as the field is not referenced as a member of class C20, then no compiler error occurs. Although line 13 may appear to generate the compiler error it does not, because "name" is accessed directly as a member of interface I20. Therefore, the compiler does not encounter an ambiguity.  
15e  Compile-time error.  A method argument will not be implicitly narrowed from type double to type float.  
16f  Compiler Error  The two-parameter constructor of U does not invoke the two-parameter constructor of T. Therefore, the constructor of U will try to invoke a no-parameter constructor of T, but none exists.  
17a  Prints: AAA  Class A has only one implementation of method m1. Class B overloads method m1 with a second implementation. Class C overloads method m1 with a third implementation. Even though b1 is an instance of class B and c1 is an instance of class C the reference type is always A so the overload methods declared in the subclasses are not accessible. For that reason, the implementation of method m1 declared in class A is always invoked.  
18c  Compiler Error.  There is no A.m2 method so polymorphism can not be used to invoke B.m2 using a reference of type A. A compiler error is generated by the attempt to invoke method a1.m2 without first casting reference a1 to type B. Method B.m2 could be invoked by first casting reference a1 to type B as follows: ((B)a1).m1().  

Copyright © 2002, Dan Chisholm
All rights reserved.
1